<template>
  <!-- 英雄联盟自动获取授权 -->
  <el-dialog :title="'扫码授权'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="300px" append-to-body @close="close">
    <div class="form-box" v-loading="loading">
      <div class="demo-ruleForm">
        <img v-if="qrCode" :src="qrCode" alt="" style="width: 200px;height: 200px;">
        <div v-else style="width: 200px;height: 200px;">

        </div>
        <div class="center">
          {{ msg == '二维码认证中' ? '已扫码，等待授权' : '请扫描二维码进行授权' }}
        </div>
        <div><el-button @click="getToken" type="primary" size="mini">我已授权</el-button></div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import autoApi from "@/api/autoProduction/information.js";
export default {
  name: "",
  props: ["fromType"],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      msg: "",
      qrCode: "",
      uuid: "",
      timer: null,
      type: "LOL",
      carrierId: "",

      checkPending: false,
      timeOutTimer: null,
      loginBody: "",
    };
  },
  created() {},
  beforeDestroy() {
    // window.clearInterval(this.timer)
  },
  methods: {
    // 打开弹窗
    open(type, carrierId) {
      this.loading = false;
      this.type = type;
      this.carrierId = carrierId;
      this.qrCode = "";
      this.checkPending = false;
      this.dialogVisible = true;
      this.getQrCode();
    },
    close() {
      this.closeTimer();
    },
    getQrCode() {
      this.loading = true;
      this.closeTimer();
      this.msg = "";
      if (this.type == "LOLM" && this.carrierId == 45989) {
        // LOL手游  微信区
        autoApi
          .getwxLolcode()
          .then((res) => {
            this.loading = false;
            if (res.code == 200) {
              this.qrCode = "data:image/png;base64," + res.data.qrcode;
              this.uuid = res.data.qrsig;
              if (!this.checkPending) this.checkLogin();
            }
          })
          .catch((err) => {
            this.loading = false;
          });
      } else {
        autoApi
          .getLOLQRcode()
          .then((res) => {
            this.loading = false;
            if (res.code == 200) {
              this.qrCode = res.data.base64Img;
              this.uuid = res.data.uuid;
              if (!this.checkPending) this.checkLogin();
            }
          })
          .catch((err) => {
            this.loading = false;
          });
      }
    },
    // 查询二维码状态
    async checkLogin() {
      this.checkPending = true;
      if (this.type == "LOLM" && this.carrierId == 45989) {
        // LOL手游  微信区
        let obj = {
          uuid: this.uuid,
        };
        try {
          const res = await autoApi.checkWXLoginLOLM(obj);
          this.checkPending = false;
          this.closeTimer();
          this.msg = res.msg;
          if (res && res.code) {
            if (
              this.dialogVisible &&
              res.code == 200 &&
              res.msg == "二维码已认证"
            ) {
              // 已授权  开始获取数据
              this.loginBody = res.data;
              this.getToken();
            } else if (
              this.dialogVisible &&
              res.code == 200 &&
              (res.msg == "二维码认证中" || res.msg == "二维码未失效")
            ) {
              // 延时1秒后查询二维码状态
              this.timeOutTimer = setTimeout(() => {
                if (this.dialogVisible) {
                  this.checkLogin();
                }
              }, 2000);
            }
          }
        } catch (e) {
          this.checkPending = false;
          if (e == "二维码已失效" && this.dialogVisible) {
            this.getQrCode();
          }
        }
      } else {
        // QQ区 和 端游
        let obj = {
          uuid: this.uuid,
          type: this.type,
        };
        try {
          const res = await autoApi.checkLoginLOLMqq(obj);
          this.checkPending = false;
          this.closeTimer();
          this.msg = res.msg;
          if (res && res.code) {
            if (
              this.dialogVisible &&
              res.code == 200 &&
              res.msg == "二维码已认证"
            ) {
              // 已授权  开始获取数据
              this.loginBody = res.data;
              this.getToken();
            } else if (
              this.dialogVisible &&
              res.code == 200 &&
              (res.msg == "二维码认证中" || res.msg == "二维码未失效")
            ) {
              // 延时1秒后查询二维码状态
              this.timeOutTimer = setTimeout(() => {
                if (this.dialogVisible) {
                  this.checkLogin();
                }
              }, 2000);
            }
          }
        } catch (e) {
          this.checkPending = false;
          if (e == "二维码已失效" && this.dialogVisible) {
            this.getQrCode();
          }
        }
      }
    },
    // 获取数据
    getToken() {
      this.loading = true;
      if (this.type == "LOLM" && this.carrierId == 45989) {
        // LOL手游  微信区
        let obj = {
          cookie: this.loginBody,
        };
        autoApi
          .getwxLolmData(obj)
          .then((res) => {
            this.loading = false;
            if (res.msg == "操作成功") {
              let resData = {
                type: this.type,
                ...res.data,
              };
              this.$emit("success", resData);
              this.dialogVisible = false;
            }
          })
          .catch((err) => {
            this.loading = false;
            if (err.code == 500) {
              this.getQrCode();
            }
          });
      } else {
        let obj = {
          cookie: this.loginBody,
          type: this.type,
        };
        autoApi
          .postToken(obj)
          .then((res) => {
            this.loading = false;
            if (res.msg == "操作成功") {
              let resData = {
                type: this.type,
                ...res.data,
              };
              this.$emit("success", resData);
              this.dialogVisible = false;
            }
          })
          .catch((err) => {
            this.loading = false;
            if (err.code == 500) {
              this.getQrCode();
            }
          });
      }
    },
    // 关闭延时器
    closeTimer() {
      if (this.timeOutTimer) clearTimeout(this.timeOutTimer);
      this.timeOutTimer = null;
    },

    extractUrlFromString(str) {
      // 匹配以http或https开头的URL
      const urlPattern = /https?:\/\/[^\s',]+/;
      const match = str.match(urlPattern);
      return match ? match[0] : null;
    },

    getWXloginCode(str) {
      const regex = /wx_code='([^']+)'/; // 匹配单引号内的内容
      const match = str.match(regex);
      const wxCode = match ? match[1] : null; // 提取捕获组
      return wxCode;
    },
  },
};
</script>
<style scoped lang="css">
.demo-ruleForm {
  text-align: center;
}
</style>
